package com.example.service;

import cn.hutool.core.collection.ListUtil;
import com.example.entity.Business;
import com.example.entity.History;
import com.example.mapper.HistoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author: 康伟奇
 * @date: 2024 - 01 - 02 15:46
 **/
@Service
public class HistoryService {
    @Resource
    private HistoryMapper historyMapper;

    @Resource
    private BusinessService businessService;

    /**
     * 新增
     */
    public void add(History history){
        historyMapper.insert(history);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        historyMapper.deleteById(id);
    }

    /**
     * 根据ID查询
     */
    public History selectById(Integer id) {
        return historyMapper.selectById(id);
    }


    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            historyMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(History history) {
        historyMapper.updateById(history);
    }

    /**
     * 查询所有
     */
    public List<History> selectAll(History history) {
        List<History> histories = historyMapper.selectAll(history);
        for (History h : histories) {
            Business business = businessService.selectById(h.getBusinessId());
            h.setBusiness(business);
        }

        histories.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
        return histories;
    }

    /**
     * 根据businessId和userId查找单个History
     * @param businessId 商家id
     * @param userId 用户id
     * @return History
     */
    public History selectByBusinessIdAndUserId(Integer businessId, Integer userId) {

        return historyMapper.selectByBusinessIdAndUserId(businessId,userId);
    }
}
